import sys
import cv2
import numpy as np
import gym_pih.hardware.TIS as TIS

class Camera():
    def __init__(self):
        
        self.tis=TIS.TIS()
        self.tis.open_device("33124075", 1280, 720, "60/1", TIS.SinkFormats.BGRA,True)
        self.tis.set_property("ExposureAuto","Off")
        self.tis.set_property("GainAuto","Off")
        self.tis.set_property("ExposureTime",33333)  #1/30s
        self.tis.set_property("Gain",0)
        # self.tis.set_property("Gain", 13.2)
        # self.tis.list_properties()             使用这句查看可以修改的属性名字，属性的可以设的值可以在相机的软件中看（比如和Off相对的其实是Continus)
        # print(self.tis.get_property("ExposureTime"))

    def Open(self):
        self.tis.start_pipeline()

    def GetImage(self):
        if self.tis.snap_image(1):
            image=self.tis.get_image()
            image=image[:,:,:3]
            done=True
        else:
            image=None
            done=False
        return done,image
    
if __name__=="__main__":
    cam=Camera()
    cam.Open()
    cv2.namedWindow("image",cv2.WINDOW_NORMAL)
    while True:
        done,image=cam.GetImage()
        image=image[:,:,:3]
        cv2.imshow("image",image)
        key = cv2.waitKey(1)
        if key == 27:  # check if the key is ESC
            cv2.destroyAllWindows()
            break
